/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.prerecieve.service;

import java.util.List;

import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.prepay.entity.Tprepayment;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.prerecieve.entity.TadvancesReceived;
import com.jeesite.modules.prerecieve.dao.TadvancesReceivedDao;
import com.jeesite.modules.prerecieve.entity.TadvancesReceivedEstimated;
import com.jeesite.modules.prerecieve.dao.TadvancesReceivedEstimatedDao;
import com.jeesite.modules.prerecieve.entity.TadvancesReceivedActual;
import com.jeesite.modules.prerecieve.dao.TadvancesReceivedActualDao;

/**
 * 预收款Service
 * @author wsm
 * @version 2021-03-22
 */
@Service
@Transactional(readOnly=true)
public class TadvancesReceivedService extends CrudService<TadvancesReceivedDao, TadvancesReceived> {
	
	@Autowired
	private TadvancesReceivedEstimatedDao tadvancesReceivedEstimatedDao;
	@Autowired
	private CodeRuleService codeRuleService;
	@Autowired
	private TadvancesReceivedActualDao tadvancesReceivedActualDao;
	@Autowired
	private TadvancesReceivedDao tadvancesReceivedDao;
	/**
	 * 获取单条数据
	 * @param tadvancesReceived
	 * @return
	 */
	@Override
	public TadvancesReceived get(TadvancesReceived tadvancesReceived) {
		TadvancesReceived entity = super.get(tadvancesReceived);
		if (entity != null){
			TadvancesReceivedEstimated tadvancesReceivedEstimated = new TadvancesReceivedEstimated(entity);
			tadvancesReceivedEstimated.setStatus(TadvancesReceivedEstimated.STATUS_NORMAL);
			entity.setTadvancesReceivedEstimatedList(tadvancesReceivedEstimatedDao.findList(tadvancesReceivedEstimated));
			TadvancesReceivedActual tadvancesReceivedActual = new TadvancesReceivedActual(entity);
			tadvancesReceivedActual.setStatus(TadvancesReceivedActual.STATUS_NORMAL);
			entity.setTadvancesReceivedActualList(tadvancesReceivedActualDao.findList(tadvancesReceivedActual));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param tadvancesReceived 查询条件
	 *  分页对象
	 * @return
	 */
	@Override
	public Page<TadvancesReceived> findPage(TadvancesReceived tadvancesReceived) {
		return super.findPage(tadvancesReceived);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param tadvancesReceived
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(TadvancesReceived tadvancesReceived) {
		if(tadvancesReceived.getIsNewRecord()){
			tadvancesReceived.setAdvancesReceivedCode(codeRuleService.getCode("releasecar002"));
			tadvancesReceived.setCreateBy(UserUtils.getUser().getLoginCode());
			tadvancesReceived.setStatus1("1");
			tadvancesReceived.setBillStatus("1");

		}
		super.save(tadvancesReceived);

		if (tadvancesReceived.getIsNewRecord()){
			codeRuleService.updateCurrentNumber("releasecar002");
		}

		// 保存 TadvancesReceived子表
		for (TadvancesReceivedEstimated tadvancesReceivedEstimated : tadvancesReceived.getTadvancesReceivedEstimatedList()){
			if (!TadvancesReceivedEstimated.STATUS_DELETE.equals(tadvancesReceivedEstimated.getStatus())){
				tadvancesReceivedEstimated.setAdvancesReceivedId(tadvancesReceived);
				if (tadvancesReceivedEstimated.getIsNewRecord()){
					tadvancesReceivedEstimated.setReceivedAmountEstimated(tadvancesReceivedEstimated.getReceivedSumActual());
					//可用金额更改
					tadvancesReceivedEstimated.setUsedAmount(tadvancesReceivedEstimated.getReceivedSumActual());
					tadvancesReceivedEstimatedDao.insert(tadvancesReceivedEstimated);
				}else{
					tadvancesReceivedEstimated.setReceivedAmountEstimated(tadvancesReceivedEstimated.getReceivedSumActual()-tadvancesReceivedEstimated.getReceivedAmountActual());
					//可用金额更改
					tadvancesReceivedEstimated.setUsedAmount(tadvancesReceivedEstimated.getReceivedAmountEstimated()-tadvancesReceivedEstimated.getLockedAmount());
					tadvancesReceivedEstimatedDao.update(tadvancesReceivedEstimated);
				}
			}else{
				tadvancesReceivedEstimatedDao.delete(tadvancesReceivedEstimated);
			}
		}
		// 保存 TadvancesReceived子表
		for (TadvancesReceivedActual tadvancesReceivedActual : tadvancesReceived.getTadvancesReceivedActualList()){
			if (!TadvancesReceivedActual.STATUS_DELETE.equals(tadvancesReceivedActual.getStatus())){
				tadvancesReceivedActual.setAdvancesReceivedId(tadvancesReceived);
				if (tadvancesReceivedActual.getIsNewRecord()){
					tadvancesReceivedActualDao.insert(tadvancesReceivedActual);
				}else{
					tadvancesReceivedActualDao.update(tadvancesReceivedActual);
				}
			}else{
				tadvancesReceivedActualDao.delete(tadvancesReceivedActual);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param tadvancesReceived
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(TadvancesReceived tadvancesReceived) {
		super.updateStatus(tadvancesReceived);
	}
	
	/**
	 * 删除数据
	 * @param tadvancesReceived
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(TadvancesReceived tadvancesReceived) {
		super.delete(tadvancesReceived);
		TadvancesReceivedEstimated tadvancesReceivedEstimated = new TadvancesReceivedEstimated();
		tadvancesReceivedEstimated.setAdvancesReceivedId(tadvancesReceived);
		tadvancesReceivedEstimatedDao.deleteByEntity(tadvancesReceivedEstimated);
		TadvancesReceivedActual tadvancesReceivedActual = new TadvancesReceivedActual();
		tadvancesReceivedActual.setAdvancesReceivedId(tadvancesReceived);
		tadvancesReceivedActualDao.deleteByEntity(tadvancesReceivedActual);
	}
	//获取列表
	@Transactional(readOnly=false)
	public List<TadvancesReceivedEstimated> findListBy(String cust , String order) {
		List<TadvancesReceivedEstimated> listBy = tadvancesReceivedDao.findListBy(cust, order);
		return  listBy;
	}
	@Transactional(readOnly=false)
	public TadvancesReceivedEstimated findDetail(String id,Long originRowNumber, String expenseType) {
	   return 	tadvancesReceivedEstimatedDao.finDetail(id,originRowNumber,expenseType);
	}
	@Transactional(readOnly=false)
	public void updateDetail(TadvancesReceivedEstimated tadvancesReceivedEstimated) {
		tadvancesReceivedEstimatedDao.update(tadvancesReceivedEstimated);
	}
	@Transactional(readOnly=false)
	public void insertDetail(TadvancesReceivedEstimated tadvancesReceivedEstimated1) {
		tadvancesReceivedEstimatedDao.insert(tadvancesReceivedEstimated1);
	}
	@Transactional(readOnly=false)
	public void insetDe(TadvancesReceivedActual tadvancesReceivedActual) {
		tadvancesReceivedActualDao.insert(tadvancesReceivedActual);
	}
	@Transactional(readOnly=false)
	public int updateFinancialRecognition(String apId, String sta) {
			int i=0;
			if("5".equals(sta)){
				TadvancesReceived tadvancesReceived =new TadvancesReceived();
				tadvancesReceived.setStatus1(sta);
				tadvancesReceived.setAdvancesReceivedId(apId);
				tadvancesReceivedDao.update(tadvancesReceived);

				i=1;
			}else {
				 i = tadvancesReceivedDao.updateFinancialRecognition(apId,sta);
				if("3".equals(sta)){
					tadvancesReceivedDao.updateStatu(apId);
				}
			}
		return i;
	}
}